--#include "data\config\activityconfig\DailyActivity\SupplyContentionConfig.lua" once
--#include "data\functions\Common\RankMgr.lua" once 		--排行榜管理

--活动是否开始
function IsSupplyContentionStart()
	local sysVar = System.getDyanmicVar()
	if sysVar.SupplyContentionStartTime and sysVar.SupplyContentionStartTime > 0 then
		return true
	end
	return false
end

--活动判断
function IsSupplyContention(sysarg)
	local mySceneId =  Actor.getSceneId(sysarg)
	if(mySceneId == SupplyContentionConfig.unionTeams[1].EnterPos[1])then
		return true
	end
	return false
end

--活动开始
function SupplyContentionStar()	
	local cfg = SupplyContentionConfig
	
	gRankMgr_Clear(RANK_SUPPLYCONT_TOTAL)
	gRankMgr_Clear(RANK_SUPPLYCONT_PERSONAL)

	local sysVar = System.getDyanmicVar()
	sysVar.SupplyContentionStartTime = System.getCurrMiniTime()

	SupplyContentionMob()

	OpenSupplyContentionRightPanelAll(nil, true)

	System.broadcastTipmsg(Lang.ScriptTips.SupplyContention005,ttScreenCenter + ttChatWindow)
end

function SupplyContentionEndCheck()
	for idx = 1, #SupplyContentionConfig.unionTeams do
		local cfg = SupplyContentionConfig.unionTeams[idx]
		local totalScore = gRankMgr_GetValue(cfg.id, RANK_SUPPLYCONT_TOTAL) or 0
		if totalScore >= SupplyContentionConfig.campWinScore then
			SupplyContentionEnd()	
			return true
		end
	end
	return false
end


--活动结束
function SupplyContentionEnd()	
	SupplyContentionClearMonster()
	local sysVar = System.getDyanmicVar()
	if sysVar.SupplyContentionStartTime and sysVar.SupplyContentionStartTime > 0 then
		
		sysVar.SupplyContentionStartTime = nil
		sysVar.supplyContentionPlayer = nil

		local nSceneId = SupplyContentionConfig.unionTeams[1].EnterPos[1]
		local winIdx = GetSupplyContentionWinCamp()
		if SupplyContentionConfig.unionTeams[winIdx] then
			System.broadcastTipmsg(string.format(Lang.ScriptTips.SupplyContention006, SupplyContentionConfig.unionTeams[winIdx].name),ttScreenCenter + ttChatWindow)
		else
			System.broadcastTipmsg(Lang.ScriptTips.SupplyContention011,ttScreenCenter + ttChatWindow)
		end

		SupplyContentionAward()

		local playerList = LuaHelp.getSceneActorListById(nSceneId)  --获取在线玩家对象列表	
		if(playerList == nil)then
			return
		end
		for k,v in pairs(playerList)do	--踢出场景
			Actor.enterScene(v,unpack(SupplyContentionConfig.ExitPos))	
			SendActivityLog(v, ACTIVITY_DEFINE_SUPPLY_CONTENTION, ActLogStatus_Complete)
		end
	end
end

--上交水晶
function SupplyContentionExchange(sysarg, npcId)
	if not IsSupplyContentionStart() then
        return 
    end

    if not IsSupplyContention(sysarg) then
        return
    end

    local camp = SupplyContentionGetCamp(sysarg)
    if not camp or camp == 0 then
    	return
    end
   	if not SupplyContentionConfig.unionTeams[camp] then
   		return
   	end

   	if SupplyContentionConfig.unionTeams[camp].npcId ~= npcId then
   		Actor.sendTipmsg(sysarg, Lang.ScriptTips.SupplyContention008, ttFlyTip)
   		return 
   	end
   	
   	local dVar = System.getDyanmicVar()
	if dVar.supplyContentionPlayer and dVar.supplyContentionPlayer ~= Actor.getHandle(sysarg) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.SupplyContention012, ttFlyTip)
		return
	end

	for k,v in ipairs(SupplyContentionConfig.killBossAddBuffList) do
		if not Actor.hasBuffById(sysarg, v) then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.SupplyContention012, ttFlyTip)
			return
		end
	end

   	local AddScore = SupplyContentionConfig.addScore
   	local campAddScore = SupplyContentionConfig.addCampScore
   

   	local nActorId = Actor.getIntProperty( sysarg, PROP_ENTITY_ID ) 

	local rankingPtr = Ranking.getRanking( RankList[RANK_SUPPLYCONT_PERSONAL].strName )
	local rankCount = Ranking.getRankItemCount(rankingPtr)

	--增加个人榜积分
	gRankMgr_AddValue(nActorId, RANK_SUPPLYCONT_PERSONAL, AddScore)

	--增加一次采集成功次数
	gRankMgr_AddValueEx(nActorId, RANK_SUPPLYCONT_PERSONAL, 5, 1)

	--增加行会榜积分
	local unionName = SupplyContentionConfig.unionTeams[camp].name
	gRankMgr_AddValue(camp, RANK_SUPPLYCONT_TOTAL, AddScore, unionName)

	Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.SupplyContention007,AddScore), ttTipmsgWindow)


	local nSceneId = SupplyContentionConfig.unionTeams[1].EnterPos[1]
	local playerList = LuaHelp.getSceneActorListById(nSceneId)  --获取在线玩家对象列表	
	if playerList then
		for k,v in pairs(playerList)do   --给同阵营的加积分
			local PlayerId = Actor.getActorId(v)
			local item = Ranking.getItemPtrFromId( rankingPtr, PlayerId )
			if item then
				local everyCamp = tonumber(Ranking.getSub(item, 2))
				if nActorId ~= PlayerId and everyCamp == camp then
					Ranking.updateItem( rankingPtr, PlayerId, campAddScore )
					--增加行会榜积分
					gRankMgr_AddValue(camp, RANK_SUPPLYCONT_TOTAL, campAddScore, unionName)
					Actor.sendTipmsg(PlayerPtr, string.format(Lang.ScriptTips.SupplyContention007,campAddScore), ttTipmsgWindow)
				end
			end	
		end
	end

	if SupplyContentionEndCheck() then
		return
	end

	dVar.supplyContentionPlayer = nil
	SupplyContentionMob()
	SupplyContentionClearBuff(sysarg)
	
	ChangeSupplyContentionRightPanelAll(nil, true)
end

--打开面板
function SupplyContentionOpenAwardsPanel(sysarg)
	Actor.openDialogs(sysarg, enLinkSupplyContentionAwardWnd)
end

--进入活动
function SupplyContentionEnter(sysarg)
	local zyVal = SupplyContentionGetCamp(sysarg)
	if not zyVal then
		zyVal = SupplyContentionGetMyCamp(sysarg)
		SupplyContentionSetCamp(sysarg, zyVal)
	end

	local cfg = SupplyContentionConfig.unionTeams[zyVal]
	if not cfg then
		return
	end

	Actor.enterScene(sysarg,unpack(cfg.EnterPos))

	Actor.setCampId(sysarg, zyVal)
	Actor.setPkMode(sysarg, fpZY)
	Actor.updateActorName(sysarg)

	OpenSupplyContentionRightPanel(sysarg)
	SupplyContentionSendNotice(sysarg)

	--注意要用enDailyOpTimes_XXX系列的定义
	--检查每天最大进行N次
	if CheckActorDailyDoneTimes( sysarg, enDailyOpTimes_SupplyContent, 0, 1 ) then
		AddActorDailyDoneTimes( sysarg, enDailyOpTimes_SupplyContent, 0, 1 )
		--print("SupplyContentionEnter, first do...")
	end
end

--获取阵营
function SupplyContentionGetCamp(sysarg)
	local actorid = Actor.getActorId(sysarg)
	local value = gRankMgr_GetValueById(actorid, RANK_SUPPLYCONT_PERSONAL, 2) or 0
	if value and value > 0 then
		return value
	end
end

--计算阵营
function SupplyContentionGetMyCamp(sysarg)
	local index = 0
	local curNum = 0
	for idx = 1, #SupplyContentionConfig.unionTeams do
		local totalPower = gRankMgr_GetValueById(idx, RANK_SUPPLYCONT_TOTAL, 2) or 0
		--print("totalPower ="..totalPower)
		if curNum == 0 and index == 0 then
			index = idx
			curNum = totalPower
		elseif curNum > totalPower  then
			curNum = totalPower
			index = idx
		end
	end
	--print("index ="..index)
	return index
end

--设置阵营
function SupplyContentionSetCamp(sysarg, index)
	local actorid = Actor.getActorId(sysarg)
	gRankMgr_SetRankEx(actorid, RANK_SUPPLYCONT_PERSONAL, 2, index)
	local vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
	local sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
	local level = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)

	gRankMgr_SetRankEx(actorid, RANK_SUPPLYCONT_PERSONAL, 6, vocation)
	gRankMgr_SetRankEx(actorid, RANK_SUPPLYCONT_PERSONAL, 7, sex)
	gRankMgr_SetRankEx(actorid, RANK_SUPPLYCONT_PERSONAL, 8, level)

	local power = Actor.getIntProperty(sysarg, PROP_ACTOR_BATTLE_POWER)
	local totalPower = gRankMgr_GetValueById(index, RANK_SUPPLYCONT_TOTAL, 2) or 0
	totalPower = totalPower + power
	--print("totalPower ="..totalPower)
	local unionName = SupplyContentionConfig.unionTeams[index].name
	gRankMgr_SetRankEx(index, RANK_SUPPLYCONT_TOTAL, 2, totalPower, unionName)
end
 
--离开场景
function SupplyContentionLeaveActivityHandler(sysarg)
	local sysVar = System.getDyanmicVar()
	if sysVar.supplyContentionPlayer and sysVar.supplyContentionPlayer == Actor.getHandle(sysarg) then
		sysVar.supplyContentionPlayer = nil
		SupplyContentionMob()
		SupplyContentionSendNoticeAll(sysarg)
	end
	Actor.setCampId(sysarg, 0)
	Actor.updateActorName(sysarg)
	Actor.setPkMode(sysarg, fpPeaceful)	
	CloseActivityRightPanel(sysarg)	
	SupplyContentionClearDieBuff(sysarg)
	SupplyContentionClearBuff(sysarg)
	SupplyContentionClearScore(sysarg)
end

function SupplyContentionClearScore(sysarg)
	local nActorId = Actor.getActorId(sysarg)
	gRankMgr_SetRank(nActorId, RANK_SUPPLYCONT_PERSONAL, 0)
end

--离开活动场景
function SupplyContentionLeaveActivity(sysarg)
	Actor.enterScene(sysarg,unpack(SupplyContentionConfig.ExitPos))
end

--杀人加分 sysarg = 被杀者
function SupplyContentionActorDeath( sysarg, killerPtr )
	--BaseFuc_Print("Enter SupplyContentionActorDeath")
	if(sysarg == nil or killerPtr == nil)then
		return
	end

    if not IsSupplyContention(sysarg) then
        return
    end

	if Actor.isMonster(sysarg) then
		return
	end
	local sysVar = System.getDyanmicVar()
	if Actor.isMonster(killerPtr) then
		if sysVar.supplyContentionPlayer and sysVar.supplyContentionPlayer == Actor.getHandle(sysarg) then
			SupplyContentionMob()
			sysVar.supplyContentionPlayer = nil
			SupplyContentionClearBuff(sysarg)
			SupplyContentionSendNoticeAll()
		end
		return
	end

	local myCamp = Actor.getIntProperty(sysarg, PROP_ACTOR_ZY)
	local killerCamp = Actor.getIntProperty(killerPtr, PROP_ACTOR_ZY)
	if myCamp == 0 or killerCamp == 0 or myCamp == killerCamp then
		return
	end

	SupplyContentionClearDieBuff(killerPtr)
	local killerId = Actor.getActorId(killerPtr)
	gRankMgr_SetRankEx(killerId, RANK_SUPPLYCONT_PERSONAL, 1, 0)
	gRankMgr_AddValueEx(killerId, RANK_SUPPLYCONT_PERSONAL, 3, 1)
	local killCount = gRankMgr_GetValueById(killerId, RANK_SUPPLYCONT_PERSONAL, 3) or 0
	for idx = 1, #SupplyContentionConfig.killBroadCast do
		local broadCastCfg = SupplyContentionConfig.killBroadCast[idx]
		local isTheLast = false
		if idx == #SupplyContentionConfig.killBroadCast then
			isTheLast = true
		end
		if broadCastCfg.broadcast and broadCastCfg.broadcast ~= "" and (broadCastCfg.killCount == killCount or (isTheLast and broadCastCfg.killCount < killCount) ) then
			local killerName = gRankMgr_GetValueById(killerId, RANK_SUPPLYCONT_PERSONAL, 0)
			System.broadcastTipmsg(string.format(broadCastCfg.broadcast, killerName),ttScreenCenter + ttChatWindow)
		end
	end

	local actorId = Actor.getActorId(sysarg)
	local sysargPoint = gRankMgr_GetValue(actorId, RANK_SUPPLYCONT_PERSONAL)
	local nFactor = System.getRandomNumber(10000)

	local killAddPoint = 0

	for idx = 1, #SupplyContentionConfig.killScore do
		local value = SupplyContentionConfig.killScore[idx]
		if Actor.checkActorLevel(sysarg, value.level, value.circle)then
			killAddPoint = value.addScore
			break
		end
	end

	for idx = 1, #SupplyContentionConfig.killScoreRate do
		local cfg = SupplyContentionConfig.killScoreRate[idx]
		if nFactor < cfg.rate then
			killAddPoint = math.floor(killAddPoint + sysargPoint * cfg.addKillScoreRate / 100)
			break
		end
	end

	gRankMgr_AddValueEx(actorId, RANK_SUPPLYCONT_PERSONAL, 1, 1)
	gRankMgr_AddValueEx(actorId, RANK_SUPPLYCONT_PERSONAL, 4, 1)

	if killAddPoint > 0 then
		--增加个人榜积分
		gRankMgr_AddValue(killerId, RANK_SUPPLYCONT_PERSONAL, killAddPoint)

		--增加行会榜积分
		local index = SupplyContentionGetCamp(killerPtr)
		if index and index > 0 then
			local unionName = SupplyContentionConfig.unionTeams[index].name
			gRankMgr_AddValue(index, RANK_SUPPLYCONT_TOTAL, killAddPoint, unionName)
		end

		Actor.sendTipmsg(killerPtr, string.format(Lang.ScriptTips.SupplyContention007,killAddPoint), ttTipmsgWindow)
	end

	if SupplyContentionEndCheck() then
		return
	end

	if sysVar.supplyContentionPlayer and sysVar.supplyContentionPlayer == Actor.getHandle(sysarg) then
		SupplyContentionMob()
		sysVar.supplyContentionPlayer = nil
		SupplyContentionClearBuff(sysarg)
		ChangeSupplyContentionRightPanelAll(nil, true)
	else
		ChangeSupplyContentionRightPanelAll(killerPtr, false)
	end
end

--复活
function SupplyContentionRelive(sysarg, reliveType)
	if reliveType and reliveType == 2 then
		local zyVal = SupplyContentionGetCamp(sysarg)

		local cfg = SupplyContentionConfig.unionTeams[zyVal]
		if not cfg then
			return
		end

		Actor.enterScene(sysarg,unpack(cfg.EnterPos))
	end

	local actorId = Actor.getActorId(sysarg)
	local count = gRankMgr_GetValueById(actorId, RANK_SUPPLYCONT_PERSONAL, 1)  or 0
	SupplyContentionAddDieBuff(sysarg, count)
end

--获取胜利方
function GetSupplyContentionWinCamp()
	local winIdx = 0
	local winScore = 0
	local winCount = 0
	for idx = 1, #SupplyContentionConfig.unionTeams do
		local cfg = SupplyContentionConfig.unionTeams[idx]
		local campScore = gRankMgr_GetValue(idx, RANK_SUPPLYCONT_TOTAL)
		if winIdx == 0 then
			winIdx = idx
			winScore = campScore
			winCount = 1
		elseif winScore < campScore then
			winIdx = idx
			winScore = campScore
			winCount = 1
		elseif winScore == campScore then
			winCount = winCount + 1
		end
	end
	if winCount <= 1 then
		return winIdx
	end
	return 0
end


--打开所有人的左侧面板
function OpenSupplyContentionRightPanelAll(sysarg, needSendProtocol)
	local playerList = LuaHelp.getSceneActorListById( SupplyContentionConfig.unionTeams[1].EnterPos[1] ) or {}
	for i,v in ipairs(playerList) do
		OpenSupplyContentionRightPanel(v)
		if needSendProtocol then
			SupplyContentionSendNotice(v)
		elseif sysarg == v then
			SupplyContentionSendNotice(v)
		end
	end
end

--改变所有人的左侧面板
function ChangeSupplyContentionRightPanelAll(sysarg, needSendProtocol)
	local playerList = LuaHelp.getSceneActorListById( SupplyContentionConfig.unionTeams[1].EnterPos[1] ) or {}
	for i,v in ipairs(playerList) do
		ChangeSupplyContentionRightPanel(v)
		if needSendProtocol then
			SupplyContentionSendNotice(v)
		elseif sysarg == v then
			SupplyContentionSendNotice(v)
		end
	end
end

--协议通知
function SupplyContentionSendNoticeAll(sysarg)
	local playerList = LuaHelp.getSceneActorListById( SupplyContentionConfig.unionTeams[1].EnterPos[1] ) or {}
	for i,v in ipairs(playerList) do
		if sysarg and sysarg == v then
		else
			SupplyContentionSendNotice(v)
		end
	end
end

--打开左侧面板
function OpenSupplyContentionRightPanel(sysarg)

	local panelInfo = {}
	
	panelInfo.sts = 1
	
	panelInfo.title = Lang.ScriptTips.SupplyContentionPanel001

	local sysVar = System.getDyanmicVar()

	panelInfo.timeTitle = Lang.ScriptTips.SupplyContentionPanel002
	panelInfo.restTime = sysVar.SupplyContentionStartTime + SupplyContentionConfig.Time - System.getCurrMiniTime()

	panelInfo.contentTitle = ""

	local camp = SupplyContentionGetCamp(sysarg)
    if not camp or camp == 0 then
    	return
    end
   	if not SupplyContentionConfig.unionTeams[camp] then
   		return
   	end

   	local myCampScore 
   	local otherCampScore

   	for idx = 1, #SupplyContentionConfig.unionTeams do
   		local cfg = SupplyContentionConfig.unionTeams[idx]
   		if cfg.id == camp then
   			myCampScore = gRankMgr_GetValue(cfg.id, RANK_SUPPLYCONT_TOTAL)
   		else
   			otherCampScore = gRankMgr_GetValue(cfg.id, RANK_SUPPLYCONT_TOTAL)
   		end
   	end


	panelInfo.contents = {}
	panelInfo.contents[1] = string.format(Lang.ScriptTips.SupplyContentionPanel003, myCampScore, otherCampScore)
	panelInfo.contents[2] = Lang.ScriptTips.SupplyContentionPanel004
	
	panelInfo.awardTitle = ""
	panelInfo.awards = {}

	panelInfo.buttons = {1, 4}

	local panelType = 2
	local activityId = enRightPanelActivity_SupplyContent
	OpenActivityRightPanel( sysarg, panelType, activityId, panelInfo )

end

--左侧面板改变
function ChangeSupplyContentionRightPanel(sysarg)

	local panelInfo = {}

	panelInfo.sts = 1

	panelInfo.contentTitle = ""

	local camp = SupplyContentionGetCamp(sysarg)
    if not camp or camp == 0 then
    	return
    end
   	if not SupplyContentionConfig.unionTeams[camp] then
   		return
   	end

   	local myCampScore 
   	local otherCampScore

   	for idx = 1, #SupplyContentionConfig.unionTeams do
   		local cfg = SupplyContentionConfig.unionTeams[idx]
   		if cfg.id == camp then
   			myCampScore = gRankMgr_GetValue(cfg.id, RANK_SUPPLYCONT_TOTAL)
   		else
   			otherCampScore = gRankMgr_GetValue(cfg.id, RANK_SUPPLYCONT_TOTAL)
   		end
   	end


	panelInfo.contents = {}
	panelInfo.contents[1] = string.format(Lang.ScriptTips.SupplyContentionPanel003, myCampScore, otherCampScore)
	panelInfo.contents[2] = Lang.ScriptTips.SupplyContentionPanel004
	

	panelInfo.buttons = {1, 4}

	ChangeActivityRightPanel( sysarg, panelInfo )
end

function SupplyContentionMonDie(monster, sysarg, monId)
	if sysarg and Actor.getEntityType(sysarg) == 0 then
		local dVar = System.getDyanmicVar()
		dVar.supplyContentionPlayer = Actor.getHandle(sysarg)
		SupplyContentionAddBuff(sysarg)

		SupplyContentionSendNoticeAll()
	else
		SupplyContentionMob()
	end
end

function SupplyContentionMob()
	freshSceneMonsterInPosForce(SupplyContentionConfig.boss)
end

function SupplyContentionClearMonster()
	local scenePtr = Fuben.getSceneById(SupplyContentionConfig.unionTeams[1].EnterPos[1], 0)
	if scenePtr then
		Fuben.clearSceneAllMonsters(scenePtr)
	end
end

function SupplyContentionAddBuff(sysarg)
	for k,v in ipairs(SupplyContentionConfig.killBossAddBuffList) do
		Actor.addBuffById(sysarg, v)
	end
end

function SupplyContentionClearBuff(sysarg)
	for k,v in ipairs(SupplyContentionConfig.killBossAddBuffList) do
		Actor.delBuffById(sysarg, v)
	end
end

--清除buff
function SupplyContentionClearDieBuff(sysarg)
    for k,v in ipairs(SupplyContentionConfig.deadBuff) do
        for x,y in ipairs(v.buffId or {}) do
			Actor.delBuffById(sysarg, y)
		end
	end
end

function SupplyContentionAddDieBuff(sysarg, count)
	for idx = 1, #SupplyContentionConfig.deadBuff do
		local cfg = SupplyContentionConfig.deadBuff[idx]
		if count == cfg.deadCount then
			for ii = 1, #cfg.buffId do
				Actor.addBuffById(sysarg, cfg.buffId[ii])
			end
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.SupplyContention009, ttChatWindow)	
			break
		end
	end
end

function SupplyContentionSendNotice(sysarg)
	local isSupply = 0
	local ActorName = ""
	local ActorSex = -1
	local ActorVocation = 0
	local ActorCamp = 0
	local myCamp = SupplyContentionGetCamp(sysarg)
	local dVar = System.getDyanmicVar()
	if dVar.supplyContentionPlayer then
		local player = Actor.getEntity(dVar.supplyContentionPlayer)
		if player then
			isSupply = 1
			ActorName = Actor.getName(player)
			ActorSex = Actor.getIntProperty( player, PROP_ACTOR_SEX ) 
			ActorVocation = Actor.getIntProperty( player, PROP_ACTOR_VOCATION ) 
			ActorCamp = Actor.getIntProperty( player, PROP_ACTOR_ZY ) 
		end
	end
	local actorId = Actor.getActorId(sysarg)
	local myScore = gRankMgr_GetValue(actorId, RANK_SUPPLYCONT_PERSONAL)
	local pack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sSupplyContentionNotice)
	if (pack == nil) then
		return
	end
	DataPack.writeByte(pack, isSupply)
	if isSupply == 1 then
		DataPack.writeString(pack, ActorName or "") 
		DataPack.writeByte(pack, ActorVocation or 0)
		DataPack.writeChar(pack, ActorSex or -1)
		DataPack.writeChar(pack, ActorCamp or 0)
	end
	DataPack.writeInt(pack, myScore or 0)
	DataPack.writeChar(pack, myCamp or 0)
	DataPack.flush(pack)
end

function SupplyContentionGetRankData(sysarg)
	local rankName = RankList[RANK_SUPPLYCONT_PERSONAL].strName
	local rankingPtr = Ranking.getRanking( rankName )
	if (rankingPtr == nil) then
		return
	end
	local num = SupplyContentionConfig.FinialRankShowNum
	local itemNum = Ranking.getRankItemCountByLimit(rankingPtr, SupplyContentionConfig.rankLimit)
	if num < itemNum then
		itemNum = num
	end
	local pack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sSupplyContentionInitData)
	if (pack == nil) then
		return
	end
	local rankLimit = SupplyContentionConfig.rankLimit
	--我的数据
	local myId = Actor.getActorId(sysarg)
	local myScore 	= 0
	local myIdx 	= 0
	local myCamp = 0
	local myKillCount = 0
	local myDieCount = 0
	local myMonsterCount = 0
	local item = Ranking.getItemPtrFromId( rankingPtr, myId)
	if item then
		myScore = Ranking.getPoint(item) or 0
		if myScore < rankLimit then			--未上榜
			myIdx = 0
		else
			myIdx = Ranking.getIndexFromPtr(item) + 1
		end
		myCamp = tonumber(Ranking.getSub(item, 2))
		myKillCount = tonumber(Ranking.getSub(item, 3))
		myDieCount = tonumber(Ranking.getSub(item, 4))
		myMonsterCount = tonumber(Ranking.getSub(item, 5))
	end	

	if itemNum > 0 then
		DataPack.writeByte(pack, itemNum)
		for idx=1, itemNum do
			local rankItem = Ranking.getItemFromIndex(rankingPtr, idx-1)
			if rankItem then
				local value = Ranking.getPoint(rankItem)
				local name = Ranking.getSub(rankItem, 0)
				local camp = tonumber(Ranking.getSub(rankItem, 2))
				local killCount = tonumber(Ranking.getSub(rankItem, 3))
				local dieCount = tonumber(Ranking.getSub(rankItem, 4))
				local monsterCount = tonumber(Ranking.getSub(rankItem, 5))
				DataPack.writeString(pack, name)
				DataPack.writeUInt(pack, value)
				DataPack.writeByte(pack, camp)
				DataPack.writeShort(pack, killCount)
				DataPack.writeShort(pack, dieCount)
				DataPack.writeByte(pack, monsterCount)
			end
		end
	else
		DataPack.writeByte(pack, 0)
	end

	DataPack.writeWord(pack, myIdx)
	DataPack.writeUInt(pack, myScore)
	DataPack.writeByte(pack, myCamp)
	DataPack.writeShort(pack, myKillCount)
	DataPack.writeShort(pack, myDieCount)
	DataPack.writeByte(pack, myMonsterCount)
	

	DataPack.flush(pack)
end

function SupplyContentionGetPoint(sysarg)
	local dVar = System.getDyanmicVar()
		
	if dVar.supplyContentionPlayer then
		local player = Actor.getEntity(dVar.supplyContentionPlayer)
		if player then
			local x,y = Actor.getEntityPosition(player, 0, 0)
			local pack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sSupplyContentionGetPoint)
			if (pack == nil) then
				return
			end
			DataPack.writeShort(pack, x or 0)
			DataPack.writeShort(pack, y or 0)
		
			DataPack.flush(pack)
		end
	end
end


--发送奖励
function SupplyContentionAward()
	local winCamp = GetSupplyContentionWinCamp()
	local rankName = RankList[RANK_SUPPLYCONT_PERSONAL].strName
	local rankingPtr = Ranking.getRanking( rankName )
	if (rankingPtr == nil) then
		return
	end
	local itemNum = Ranking.getRankItemCount(rankingPtr)
	local firstPlayerAward = true
	for idx = 1, itemNum do
		local item = Ranking.getItemFromIndex( rankingPtr, idx - 1 )
		if item then
			local actorId = Ranking.getId(item)
			local point = Ranking.getPoint(item)
			local camp = tonumber(Ranking.getSub(item, 2))
			local vocation = tonumber(Ranking.getSub(item, 6))
			local sex = tonumber(Ranking.getSub(item, 7))
			local level = tonumber(Ranking.getSub(item, 8))
			local sysarg = Actor.getActorById(actorId)
			if sysarg then
				level = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
			end
			if firstPlayerAward and camp == winCamp then
				firstPlayerAward = false
				local rewards = TransAwardsByType(SupplyContentionConfig.winnerFirst, level)
				local title = Lang.ScriptTips.SupplyContention013
				local content = Lang.ScriptTips.SupplyContention014
				Awards.GiveToMailById(actorId, rewards, title, content, LogIds.SupplyContentionLogId, Lang.LogTips.SupplyContention, vocation, sex)
			end
			local awards = SupplyContentionGetAwardIdx(point, level)
			if awards and #awards > 0 then
				local title = Lang.ScriptTips.SupplyContention017
				local content = string.format(Lang.ScriptTips.SupplyContention018, point)
				Awards.GiveToMailById(actorId, awards, title, content, LogIds.SupplyContentionLogId, Lang.LogTips.SupplyContention, vocation, sex)
			end
			if camp == winCamp and point >= SupplyContentionConfig.rankLimit then
				local rewards = TransAwardsByType(SupplyContentionConfig.winnerAwards, level)
				local title = Lang.ScriptTips.SupplyContention015
				local content = Lang.ScriptTips.SupplyContention016
				Awards.GiveToMailById(actorId, rewards, title, content, LogIds.SupplyContentionLogId, Lang.LogTips.SupplyContention, vocation, sex)
			end
		end
	end
end



--根据排名获得活动奖励位置
function SupplyContentionGetAwardIdx(idx, level)
	local fightingRewards = {}
	if(1 <= idx)then
		for k,v in pairs(SupplyContentionConfig.Awards)do
			if(v.condition[1] <= idx and idx <= v.condition[2])then
				fightingRewards = v.award
				break
			end
		end
	end	
	return fightingRewards
end